package cn.gwm.flink.streaming.dwm.charge.template;

import cn.gwm.flink.streaming.constant.BaseFields;
import cn.gwm.flink.streaming.dwm.charge.bean.ChargeArgs;
import cn.gwm.flink.streaming.dwm.charge.model.ChargeConstant;
import cn.gwm.flink.streaming.dwm.charge.support.ChargeStateSupport;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName ChargeStateModel
 * @Description 充电状态的模板方法
 * @Author LiangGuang
 * @Date 2023/05/11 14:19
 */
@Slf4j
public abstract class ChargeStateModel {
    public static final String END_FLAG = "endFlag";

    public void stateStart(ChargeArgs args) throws Exception {
        commonStageGenerate(args);
        stateStartSelfHandle(args);
        Thread.sleep(10);
        args.getOut().collect(args.getBandSummarState().value());

    }

    public void stateEnd(ChargeArgs args) throws Exception {
        commonStageEnd(args);
        stateEndSelfHandle(args);
        args.getOut().collect(args.getBandSummarState().value());
    }

    /**
     * 通用的充电阶段的生成
     *
     * @param args
     */
    private void commonStageGenerate(ChargeArgs args) throws Exception {
        JSONObject summar = args.getSummarState().value();
        JSONObject bandSummar = args.getBandSummarState().value();
        if (summar == null) {
            // 首次 需要生成物理充电和阶段充电
            summar = ChargeStateSupport.createSummar(args.getConstant(), args.getFirstData(),
                    args.getConnectTimeState().value(), args.getChargeModelFirstState(), args.getIsComplete());
            ChargeStateSupport.replenishSummar(summar, args);
            summar.set("type", ChargeConstant.summar);
            bandSummar = ObjectUtil.cloneByStream(summar);
            ChargeStateSupport.replenishStart(args.getConstant(), bandSummar, args.getFirstData());
            bandSummar.set("type", ChargeConstant.bandSummar);
            bandSummar.set("chargeStatus", args.getNowState().getCode());
            // 更正物理充电的开始时间
            Long connectTime = args.getConnectTimeState().value();
            summar.set(BaseFields.tid, connectTime);
            summar.set(BaseFields.PARTITION_KEY, DateUtil.format(DateUtil.date(connectTime), DatePattern.NORM_DATE_PATTERN));
        } else {
            // 新生成一个 bandSummar 信息  复用bandSummar的基础信息
            ChargeStateSupport.replenishStart(args.getConstant(), bandSummar, args.getFirstData());
            ChargeStateSupport.removeEnd(bandSummar);
            bandSummar.set("chargeStatus", args.getNowState().getCode());
            summar.set("Tbox_BattSOCLim", bandSummar.getStr("Tbox_BattSOCLim"));
            // 补充字段
//            ChargeStateSupport.replenishSummar(summar, args);
            ChargeStateSupport.replenishBandSummar(summar, bandSummar);
        }
        args.getSummarState().update(summar);
        args.getBandSummarState().update(bandSummar);

    }

    /**
     * 通用的充电阶段的生成
     *
     * @param args
     */
    private void commonStageEnd(ChargeArgs args) throws Exception {
        ChargeStateSupport.commonEnd(args);
    }

    /**
     * 子类实现自己的额外工作 【阶段开始】
     *
     * @param args
     */
    protected abstract void stateStartSelfHandle(ChargeArgs args) throws Exception;

    /**
     * 子类实现自己的额外工作 【阶段结束】
     *
     * @param args
     */
    protected abstract void stateEndSelfHandle(ChargeArgs args) throws Exception;


}
